//
//  SKUDetailTbv_V4_Price_Chart_Cell.m
//  ZITHER
//
//  Created by weixin on 2018/11/10.
//  Copyright © 2018 weixn. All rights reserved.
//

#import "SKUDetailTbv_V4_Price_Chart_Cell.h"

#define chartNoPriceHeight 0
@implementation SKUDetailTbv_V4_Price_Chart_Cell


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [self setUI];
    [self setMasonry];
    
}

-(void)setUI{
    [self addSubview:self.priceListView];
    [self addSubview:self.publicPrice];
    [self addSubview:self.scr];
  
    [self.scr addSubview:self.backView];
    [self addSubview:self.marketBtn];
    [self addSubview:self.parameterBtn];
//    [self.backView addSubview:self.secondHandPrice];
    [self secondHandPrice];
    [self.scr addSubview:self.tbvBackV];
    [self.scr addSubview:self.tbvTitle];
    [self addSubview:self.imageMark];
    
    [self.backView addSubview:self.chart];
    [self addLineView];
}

-(void)addLineView{
    BaseView *view = [[BaseView alloc]init];
    view.backgroundColor = HexColor(0xf5f5f5);
    [self addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_bottom).offset(-10);
        make.left.right.equalTo(self).offset(0);
        make.height.mas_equalTo(10);
        
    }];
}


-(void)chartData:(skuDetailModel *)model{
    if ( model.historyPrices.count == 0) {
        return;
    }

    NSMutableArray *arrDate = [[NSMutableArray alloc]init];
    NSMutableArray *arrPrice = [[NSMutableArray alloc]init];

    
//    [model.historyPrices[0].date currentDateDateFormatter:@"MM-dd"];
    
    if ( model.historyPrices.count == 1) {
        //数组排序 按时间 最新的放在最前面 _date 2018-12-28格式
        NSRange  rang1= {8,2};
        NSRange  rang2= {5,2};
        
        NSString *str = [NSString stringWithFormat:@"%@/%@",[model.historyPrices[0].date substringWithRange:rang2],[model.historyPrices[0].date substringWithRange:rang1]];
        [arrDate addObject:@""];
        [arrDate addObject:@""];
        [arrDate addObject:str];
        for (int i = 0; i<3; i++) {
             [arrPrice addObject:model.historyPrices[0].price];
        }


    }else if ( model.historyPrices.count == 2){
        NSRange  rang1= {8,2};
        NSRange  rang2= {5,2};
        
        NSString *str = [NSString stringWithFormat:@"%@/%@",[model.historyPrices[1].date substringWithRange:rang2],[model.historyPrices[1].date substringWithRange:rang1]];
        [arrDate addObject:@""];
        [arrDate addObject:str];
        str = [NSString stringWithFormat:@"%@/%@",[model.historyPrices[0].date substringWithRange:rang2],[model.historyPrices[0].date substringWithRange:rang1]];
        [arrDate addObject:str];
        for (int i = 0; i<2; i++) {
            [arrPrice addObject:model.historyPrices[1].price];
        }
        [arrPrice addObject:model.historyPrices[0].price];

    }else{
        NSRange  rang1= {8,2};
        NSRange  rang2= {5,2};
        
        NSString *str = [NSString stringWithFormat:@"%@/%@",[model.historyPrices[2].date substringWithRange:rang2],[model.historyPrices[2].date substringWithRange:rang1]];
        [arrDate addObject:str];
                str = [NSString stringWithFormat:@"%@/%@",[model.historyPrices[1].date substringWithRange:rang2],[model.historyPrices[1].date substringWithRange:rang1]];
        [arrDate addObject:str];
        str = [NSString stringWithFormat:@"%@/%@",[model.historyPrices[0].date substringWithRange:rang2],[model.historyPrices[0].date substringWithRange:rang1]];
        [arrDate addObject:str];
        [arrPrice addObject:model.historyPrices[2].price];
        [arrPrice addObject:model.historyPrices[1].price];
        [arrPrice addObject:model.historyPrices[0].price];
    
    }
    
    self.chart.allDates = [[arrDate reverseObjectEnumerator] allObjects];
    self.chart.allValues =  [[arrPrice reverseObjectEnumerator] allObjects];
    NSLog(@"");
    
}


-(void)setModel:(skuDetailModel *)model
{
    _model = model;
    
    if (model==nil) {
        return;
    }
    self.priceListView.model = model;

    [self chartData:model];
    
//    @property (nonatomic,strong)NSArray * allValues;
//    @property (nonatomic,strong)NSArray * allDates;
    int priceInt = [self.model.conjuncturePrice intValue];
    NSNumberFormatter *moneyFormatter = [[NSNumberFormatter alloc] init];
    moneyFormatter.positiveFormat = @"###,##0";
    NSString *formatString = [moneyFormatter stringFromNumber:@(priceInt)];
    if (priceInt != 0) {
        self.publicPrice.text = [NSString stringWithFormat:@"￥%@",formatString];//公价
    }else{
        self.publicPrice.text = @"暂无";
    }

    priceInt = [self.model.price intValue];
    if (priceInt != 0) {
        moneyFormatter.positiveFormat = @"###,##0";
        formatString = [moneyFormatter stringFromNumber:@(priceInt)];
//        self.secondHandPrice.text = [NSString stringWithFormat:@"￥%@",formatString]; //二手价
        self.chart.model = model;
        self.chart.hidden = NO;
        self.backView.height = (kScreenW - 30)/345.0*237-10;

    }else{
        self.chart.hidden = YES;
        self.secondHandPrice.text = @"暂无";
        [self.scr mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(chartNoPriceHeight);
        }];
        self.backView.height = chartNoPriceHeight-10;
    }
    self.tbvTitle.model = model;

    [self.scr mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_bottom).offset(-40);
    }];
    self.marketBtn.hidden = YES;
    self.parameterBtn.hidden = YES;
    self.imageMark.hidden = YES;
    self.scr.contentSize = CGSizeMake(kScreenW, 0);
//    if (model.attributes.count==0) {
//
//    }else{
//        [self.scr mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.bottom.equalTo(self.mas_bottom).offset(-70);
//        }];
//        self.marketBtn.hidden = NO;
//        self.parameterBtn.hidden = NO;
//        self.imageMark.hidden = NO;
//        self.scr.contentSize = CGSizeMake(kScreenW*2, 0);
//    }
   
    
    
}

-(void)setMasonry{
    
    [self.priceListView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(0);
        make.left.right.equalTo(self).offset(0);
        make.height.mas_equalTo(40);
    }];

    [self.imageMark mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.marketBtn.mas_centerY).offset(0);
        make.left.equalTo(self.mas_centerX).offset(-2);
        make.width.mas_equalTo(6);
        make.height.mas_equalTo(12);
    
    }];
    
    [self.marketBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.scr.mas_bottom).offset(25);
        make.right.equalTo(self.mas_centerX).offset(-15);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(16);
    }];
    
    
    [self.parameterBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.scr.mas_bottom).offset(25);
        make.left.equalTo(self.mas_centerX).offset(15);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(16);
    }];
    
    
    [self.scr mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(40+70);
        make.left.equalTo(self).offset(0);
        make.width.mas_equalTo(kScreenW);
        make.height.mas_equalTo((kScreenW - 30)/345.0*237);
        make.bottom.equalTo(self.mas_bottom).offset(-10);
    }];

}
-(BaseLab *)secondHandPrice{
    if (!_secondHandPrice) {
        _secondHandPrice = [[BaseLab alloc]initializeFrame:CGRectMake(90, 15, 100, 14) text:@"💰250，200" font:10 textColor:[UIColor blackColor] isBold:NO numberOfLines:1];
//        _secondHandPrice.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:20];
        _secondHandPrice.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:15];

        _secondHandPrice.textColor = baseColorTheme;
        [self addSecondHandPrice];
    }
    return _secondHandPrice;
}

-(void)addSecondHandPrice{

//    BaseLab * secondHandPrice = [[BaseLab alloc]initializeFrame:CGRectMake(40, 15, 50, 15) text:@"二手参考价" font:10 textColor:[UIColor blackColor] isBold:NO numberOfLines:1];
////    secondHandPrice.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:15];
//    secondHandPrice.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:15];
//    secondHandPrice.textColor = baseColorTheme;
//    [self.scr addSubview:secondHandPrice];
//
//    [secondHandPrice mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.equalTo(self.mas_centerY).offset(0);
////        make.top.equalTo(self).offset(0);
//        make.left.equalTo(self).offset(15);
//        make.height.mas_equalTo(16);
//    }];
//    [self.scr addSubview:self.secondHandPrice];
//
//    [self.secondHandPrice mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.equalTo(self.mas_centerY).offset(0);
////        make.top.equalTo(secondHandPrice.mas_bottom).offset(8);
//        make.left.equalTo(self).offset(15);
//        make.height.mas_equalTo(16);
//    }];
    
}


-(BaseLab *)publicPrice{
    if (!_publicPrice) {
        _publicPrice = [[BaseLab alloc]initializeFrame:CGRectMake(15, 486, 28, 14) text:@"💰250，200" font:10 textColor:[UIColor blackColor] isBold:NO numberOfLines:1];
//        _publicPrice.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:15];
        _publicPrice.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:15];

        [self aaddPublicPrice];
    }
    return _publicPrice;
}


-(void)aaddPublicPrice{
    
//    BaseImageV *image = [[BaseImageV alloc]initializeFrame:CGRectMake(0, 0, 1, 1) contentMode:2];
//    image.image = [UIImage imageNamed:@"sku_icon_serise"];
//    [self addSubview:image];
//
//    [image mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self).offset(5+70);
//        make.left.equalTo(self).offset(15);
//        make.width.height.mas_equalTo(16);
//    }];
    
//    BaseLab * publicPrice = [[BaseLab alloc]initializeFrame:CGRectMake(15, 486, 28, 14) text:@"公价" font:10 textColor:[UIColor blackColor] isBold:NO numberOfLines:1];
////    publicPrice.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:15];
//    publicPrice.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:15];
//
//    [self addSubview:publicPrice];
//    [publicPrice mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.equalTo(image.mas_centerY).offset(0);
//        make.left.equalTo(image.mas_right).offset(2);
//    }];
    [self addSubview:self.publicPrice];
    
    [self.publicPrice mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.priceListView.lab.mas_bottom).offset(9);
        make.left.equalTo(self).offset(15);
    }];
}

-(BaseView *)backView{
    if (!_backView) {
        _backView = [[BaseView alloc]initWithFrame: CGRectMake(10, 5, kScreenW-20, (kScreenW - 30)/345.0*237-10)];
        _backView.backgroundColor = [UIColor whiteColor];
//        [_backView setShadowRadiusWithColor:[UIColor colorWithHex:@"000000"] offset:CGSizeZero opacity:0.1 radius:10];

    }
    return _backView;
}


- (BaseBtn *)marketBtn{
    if (!_marketBtn) {
        _marketBtn = [[BaseBtn alloc]initializeFrame:CGRectMake(0, 0, 1, 1) NormalText:@"行情" NormalTextColor:[UIColor blackColor] SeleText:@"行情" SeleTextColor:[UIColor whiteColor] font:10 isBold:NO];
        _marketBtn.backgroundColor = [UIColor randomColor];
        _marketBtn.backgroundColor = [UIColor RGBColorWithR:232 g:232 b:232];
        _marketBtn.selected = YES;
        _marketBtn.backgroundColor = [UIColor blackColor];
        [_marketBtn setCornerRadiusWithRadius:8 isMasks:YES];
        @weakify(self);
        [[_marketBtn rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
//            [self markBtnClick];
            [self scrContentOffset];
        }];
    }
    return _marketBtn;
}

-(void)scrContentOffset{
    if (!self.marketBtn.selected) {
        self.scr.contentOffset = CGPointMake(0, 0);
    }else{
        self.scr.contentOffset = CGPointMake(kScreenW, 0);
    }
}

- (BaseBtn *)parameterBtn{
    if (!_parameterBtn) {
        _parameterBtn = [[BaseBtn alloc]initializeFrame:CGRectMake(0, 0, 1, 1) NormalText:@"参数" NormalTextColor:[UIColor blackColor] SeleText:@"参数" SeleTextColor:[UIColor whiteColor] font:10 isBold:NO];
//        _parameterBtn.backgroundColor = [UIColor randomColor];
        _parameterBtn.backgroundColor = [UIColor RGBColorWithR:232 g:232 b:232];

        [_parameterBtn setCornerRadiusWithRadius:8 isMasks:YES];
        @weakify(self);
        [[_parameterBtn rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
//            [self parameterBtnClick];
            [self scrContentOffset];
        }];
    }
    return _parameterBtn;
}

-(BaseScrollView *)scr
{
    if (!_scr) {
        _scr = [[BaseScrollView alloc]initializeFrame:CGRectMake(0, 0, 1, 1) contentSize:CGSizeMake(kScreenW*2, 0)];
//        [_backView setShadowRadius:10 Opacity:0.4 Color:[UIColor grayColor]];
  
        _scr.delegate = self;
//        _scr.clipsToBounds = YES;
    }
    return _scr;
    
}

-(MainDetailTopPropertyTbv *)tbvTitle{
    if (!_tbvTitle) {
        _tbvTitle = [[MainDetailTopPropertyTbv alloc]initWithFrame:CGRectMake(kScreenW+10,0 , kScreenW-20,SCREEN_WIDTH-30)];
        _tbvTitle.backgroundColor = [UIColor whiteColor];

    }
    return  _tbvTitle;
}

-(BaseImageV *)imageMark{
    if (!_imageMark) {
        _imageMark = [[BaseImageV alloc]initializeFrame:CGRectMake(0, 0, 1, 1) contentMode:1];
        _imageMark.image = [UIImage imageNamed:@"sku_行情参数（右）"];
//        _imageMark.image = [UIImage imageNamed:@"SKU_行情参数"];
    }
    return _imageMark;
}

-(SKUDetailChart *)chart{
    if (!_chart) {
        _chart = [[SKUDetailChart alloc]initWithFrame:CGRectMake(0,0,  kScreenW-30,(kScreenW - 30)/345.0*237-10)];
        _chart.clipsToBounds = YES;
        _chart.userInteractionEnabled = NO;
    
    }
    return _chart;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.x == kScreenW) {
//        [self markBtnClick];
        [self parameterBtnClick];

        CGFloat f= self.tbvTitle.contentSize.height;
        self.tbvTitle.frame = CGRectMake(kScreenW+10,10 , kScreenW-20, f);
        self.tbvBackV.frame = CGRectMake(kScreenW+9,9 , kScreenW-18, f+2);
        self.tbvBackV.backgroundColor = [UIColor randomColor];
        [self.scr mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(f+20);
        }];
        if (self.blcok) {
            self.blcok();
        }

    }
    if (scrollView.contentOffset.x == 0) {
//        [self parameterBtnClick];
        [self markBtnClick];

        CGFloat f = chartNoPriceHeight;

        int priceInt = [self.model.price intValue];
        if (priceInt != 0) {
            f = (kScreenW - 30)/345.0*237;
        }
        self.backView.height = f-10;
        [self.scr mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(f);
        }];
        if (self.blcok) {
            self.blcok();
        }

    }
}

//参数点击
-(void)parameterBtnClick{
    self.parameterBtn.selected = YES;
    self.imageMark.image = [UIImage imageNamed:@"sku_行情参数（右）"];

    if (self.parameterBtn.selected) {
        self.parameterBtn.backgroundColor = [UIColor blackColor];
    }else{
        self.parameterBtn.backgroundColor = [UIColor RGBColorWithR:232 g:232 b:232];
    }
    self.marketBtn.selected = NO;
    
    if (self.marketBtn.selected) {
        self.marketBtn.backgroundColor = [UIColor blackColor];
    }else{
        self.marketBtn.backgroundColor = [UIColor RGBColorWithR:232 g:232 b:232];
    }
}

-(void)markBtnClick{
    
    self.marketBtn.selected = YES;
    self.imageMark.image = [UIImage imageNamed:@"SKU_行情参数"];

    if (self.marketBtn.selected) {
        self.marketBtn.backgroundColor = [UIColor blackColor];
    }else{
        self.marketBtn.backgroundColor = [UIColor RGBColorWithR:232 g:232 b:232];
    }
    self.parameterBtn.selected =NO;
    
    if (self.parameterBtn.selected) {
        self.parameterBtn.backgroundColor = [UIColor blackColor];
    }else{
        self.parameterBtn.backgroundColor = [UIColor RGBColorWithR:232 g:232 b:232];
    }
}

- (BaseView *)tbvBackV{
    if (!_tbvBackV) {
        _tbvBackV = [[BaseView  alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
//        [_tbvBackV setShadowRadius:8 Opacity:0.4 Color:[UIColor grayColor]];
    }
    return _tbvBackV;
}

-(SKUDetailPriceListV *)priceListView{
    if (!_priceListView) {
        _priceListView = [[SKUDetailPriceListV alloc]init];
    }
    return _priceListView;
}
@end
